﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ManageBanking.Models
{
    public class CashierModels
    {
        OnlineBankingDataContext olbk = new OnlineBankingDataContext();
        public bool ActivateAccount(decimal total,decimal money,string idaccount,int idadmin,int id)
        {
            bool kq = true;
            if (total==0&& money>=100 )
            {
                Account_Username ac =olbk.Account_Usernames.Where(a=>a.IdAccount.Equals(idaccount.Trim())).First();
                Username us = olbk.Usernames.Where(a => a.IdUsername == id).First();
                us.Lock = false;
                Deposit_With dep = new Deposit_With();
                dep.IdAccount = idaccount;
                dep.IdUserAdmin = idadmin;
                dep.DateCreation = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                dep.Money = money;
                dep.IDType = true;

                ac.Activation = true;
                olbk.Deposit_Withs.InsertOnSubmit(dep);
                olbk.SubmitChanges();
            
            }
            else
               return  kq=false;

            return kq;
        
        }
        public bool ActivateAccountNoId(decimal total, decimal money, string idaccount, int idadmin)
        {
            bool kq = true;
            if (total == 0 && money >= 100)
            {
                Account_Username ac = olbk.Account_Usernames.Where(a => a.IdAccount.Equals(idaccount.Trim())).First();
                
                
                Deposit_With dep = new Deposit_With();
                dep.IdAccount = idaccount;
                dep.IdUserAdmin = idadmin;
                dep.DateCreation = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                dep.Money = money;
                dep.IDType = true;

                ac.Activation = true;
                olbk.Deposit_Withs.InsertOnSubmit(dep);
                olbk.SubmitChanges();

            }
            else
                return kq = false;

            return kq;

        }


        public bool DepositAcc(decimal total, decimal money, string idaccount, int idadmin)
        {
        bool kq = true;
        if (total >= 100)
        {

            Deposit_With dep = new Deposit_With();
            dep.IdAccount = idaccount;
            dep.IdUserAdmin = idadmin;
            dep.DateCreation = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
            dep.Money = money;
            dep.IDType = true;
            olbk.Deposit_Withs.InsertOnSubmit(dep);
            olbk.SubmitChanges();
        }
        else
            return kq = false;

        return kq;
        
        }
        public bool WithDrawAcc(decimal total, decimal money, string idaccount, int idadmin)
        {
            bool kq = true;
            if (total-money >= 100)
            {

                Deposit_With dep = new Deposit_With();
                dep.IdAccount = idaccount;
                dep.IdUserAdmin = idadmin;
                dep.DateCreation = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                dep.Money = money;
                dep.IDType = false;
                olbk.Deposit_Withs.InsertOnSubmit(dep);
                olbk.SubmitChanges();
            }
            else
                return kq = false;

            return kq;

        }
        public bool DeActivateAcc(decimal total, decimal money, string idaccount, int idadmin,int id)
        {
            bool kq = true;
            if (total >= 100)
            {
                Username us = olbk.Usernames.Where(a => a.IdUsername == id).First();
                if (CheckLock(id)-1 == 0)
                    us.Lock = true;

                Account_Username ac = olbk.Account_Usernames.Where(a => a.IdAccount.Equals(idaccount.Trim())).First();
                Deposit_With dep = new Deposit_With();
                dep.IdAccount = idaccount;
                dep.IdUserAdmin = idadmin;
                dep.DateCreation = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                dep.Money = total;

                dep.IDType = false;
                ac.Activation = false;

                olbk.Deposit_Withs.InsertOnSubmit(dep);
                olbk.SubmitChanges();
            }
            else
                return kq = false;

            return kq;

        }
        public List<TypeAccount> ListTypeAcc()
        {
            return olbk.TypeAccounts.ToList();
        }

        public bool AddAccount(int admin,string IdUsername, int IdType,out string idaccount)
        {
            Random rd = new Random();
            string nrd = rd.Next(1000, 9999).ToString();
            string now = DateTime.Now.ToString("ddMMyyhhmmss") + nrd;
            idaccount = now;
            bool kq = true;
            try
            {
               
                
                Username us = olbk.Usernames.Where(a => a.Username1.Equals( IdUsername)).First();

                Account_Username acc = new Account_Username();
                
                
                
                acc.IdAccount = now;
                acc.IdUsername = us.IdUsername;
                acc.IDType = IdType;
                acc.DateCreate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));

                acc.IdUserAdmin = admin;
                acc.Activation = false;
                us.Lock = false;


                olbk.Account_Usernames.InsertOnSubmit(acc);
                olbk.SubmitChanges();
            }
            catch
            {
                kq = false;
            
            }
            return kq;

        }
        public int CheckLock(int iduser)
        {
            return olbk.Account_Usernames.Where(a => a.Activation == true && a.IdUsername == iduser).Count();
        
        }


    }
}
